<template>
  <div class='container-directorys'>
    <el-card>
       <div slot="header" >
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>大数据</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 目录 -->
      <el-row>
        <el-col :span="18">
       <el-form ref="form" :model="dirform" label-width="80px" inline>
  <el-form-item label="目录名称">
    <el-input v-model="dirform.directoryName"></el-input>
  </el-form-item>
    <el-form-item label="状态">
    <el-select v-model="dirform.state" placeholder="请选择">
      <el-option label="启用" value="1"></el-option>
      <el-option label="禁用" value="0"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item>
    <el-button @click="clear()">清除</el-button>
    <el-button type="primary" @click="search()">搜索</el-button>
  </el-form-item>
  </el-form>
        </el-col>
        <el-col :span="6">
           <el-button type="success" icon="el-icon-edit" style="float:right" size="small" @click="openDialog()">新增技巧</el-button>
        </el-col>
      </el-row>
     <!-- 消息提示 -->
    <el-alert
    :title="`数据一共${total}条`"
    type="info"
    :closable="false"
    show-icon
    style="margin:10px"
    >
  </el-alert>
  <!--表格 -->
   <el-table :data="directoryList">
     <el-table-column label="序号" :index="indexMethod" type="index"></el-table-column>
     <el-table-column label="所属学科" prop="subjectName"></el-table-column>
     <el-table-column label="目录名称" prop="directoryName"></el-table-column>
     <el-table-column label="创建者" prop="username"></el-table-column>
     <el-table-column label="创建日期">
       <template #default="{row}">
       {{row.addDate|parseTimeByString}}
       </template>
     </el-table-column>
     <el-table-column label="面试题数量" prop="totals"></el-table-column>
     <el-table-column label="状态">
       <template #default="{row}">
       {{row.state===1 ? '已启用': '已禁用'}}
       </template>
     </el-table-column>
     <el-table-column label="操作">
       <template #default="{row}">
         <el-link type="primary" @click="togglestate(row)">{{row.state===1 ? '启用': '禁用'}}</el-link>
         <el-link type="primary" @click="openDialog(row)" :disabled="row.state===1" >修改</el-link>
         <el-link type="primary" @click="delList(row)"  :disabled="row.state==1||row.totals>0">删除</el-link>
       </template>
     </el-table-column>
   </el-table>
   <!-- 分页 -->
     <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="dirform.page"
      :page-sizes="[5, 10, 15,20]"
      :page-size="dirform.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="float:right"
      >
    </el-pagination>
    <!-- 新增和修改的对话框 -->
    <directorys-add ref="dirref" :data="currDirectory" @updateList="updateList"></directorys-add>
    </el-card>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/directorys.js'
import directorysAdd from '../components/directorys-add.vue'
export default {
  components: { directorysAdd },
  name: 'Directorys',
  data () {
    return {
      dirform: {
        directoryName: null,
        state: null,
        page: 1, // 页码
        pagesize: 10, // 页码数量
        subjectID: null
      },
      directoryList: [], // 目录数据
      total: 0, // 总数
      currDirectory: {}// 修改的数据
    }
  },
  created () {
    this.GetDirectoryList()
  },
  methods: {
    // 获取数据
    async GetDirectoryList () {
      const { data } = await list(this.dirform)
      this.directoryList = data.items
      this.total = data.counts
    },
    // 页面数量
    handleSizeChange (val) {
      this.dirform.pagesize = val
      this.GetDirectoryList()
    },
    // 页码
    handleCurrentChange (val) {
      this.dirform.page = val
      this.GetDirectoryList()
    },
    // 重置页码
    indexMethod (index) {
      return index + (this.dirform.page - 1) * this.dirform.pagesize + 1
    },
    // 清除
    clear () {
      this.dirform = {
        directoryName: null,
        state: null,
        page: 1, // 页码
        pagesize: 10, // 页码数量
        subjectID: null
      }
    },
    // 搜索
    search () {
      this.dirform.page = 1
      this.GetDirectoryList()
    },
    // 切换状态
    async togglestate (directory) {
      await changeState({ id: directory.id, state: directory.state === 1 ? 0 : 1 })
      this.$message.success('操作成功')
      directory.state = directory.state === 1 ? 0 : 1
    },
    // 新增对话框
    openDialog (directory = {}) {
      // 新增是没有数据的，修改有数据
      this.currDirectory = directory
      this.$nextTick(() => {
        this.$refs.dirref.open()
      })
    },
    // 重新渲染新增和修改的页面
    updateList () {
      // 没有id是添加数据
      if (!this.currDirectory.id) {
        this.dirform.page = 1
      }
      this.GetDirectoryList()
    },
    // 删除数据
    delList (directory) {
      this.$confirm('将永久删除该学科吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(directory)
        // 提示
        this.$message.success('删除成功')
        // 重新渲染
        this.GetDirectoryList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.el-link{
   padding: 5px;
}
.container-directorys{
  padding: 10px;
}
</style>
